package com.os.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Random;

/**
 * @Author Matt
 * @Date 2022-01-23
 * @Desc:
 * @Version V1.0
 */
@Controller
public class SseController {

    /**
     * 使用输出媒体类型为text/event-stream，这是服务器端SSE的支持，演示每5second向浏览器推送随机消息
     * @return
     */
    @RequestMapping(value = "/push", produces = "text/event-stream")
    @ResponseBody
    public String push(){
        Random random = new Random();
        try {
            Thread.sleep(5000);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "data:Testing 1,2,3" + random.nextInt() + "\n\n";
    }
}
